package www.nfin.ya.super_filter_chain;

import www.nfin.ya.entity.excel.CellEntity;
import www.nfin.ya.utils.SuperOracleUtils;
import www.nfin.ya.utils.TableInfoExtractUtils;
import www.nfin.ya.utils.impl.OracleTableInfoExtractUtils;

import java.util.HashMap;
import java.util.Map;

public class ValueFilter extends CustomExcelShowFilter {
    TableInfoExtractUtils tableInfos = new OracleTableInfoExtractUtils();
    public ValueFilter(CustomExcelShowFilter next) {
        super(next);
        tableInfos.reload(SuperOracleUtils.initDefaultConnection());
    }
//    fin_b_bill_in  billinno
//            tableInfos.getColumnComments("fin_b_bill_in","billinno")
    @Override
    protected boolean dealt(CellEntity cellEntity, Map rule) {
        Map<String,String> mapper = new HashMap(){{
            put("comment",tableInfos.getColumnComments(cellEntity.getTableName(), cellEntity.getColumnName()));
            put("value",cellEntity.getValue());
        }};
        rowValueEdit(cellEntity, rule, mapper);

        return true;
    }

    private void rowValueEdit(CellEntity cellEntity, Map rule, Map<String, String> mapper) {

        String rule_comment = (String) rule.get(cellEntity.isColumn() ? "rowValue":"value");
        if(rule_comment != null){
            String comment = SuperOracleUtils.instead(rule_comment, mapper);
            System.out.println("instead place:"+rule_comment);
            if(SuperOracleUtils.verifySql(comment)){
                System.out.println("execute sql："+comment);
                comment = SuperOracleUtils.executeQueryCatchFirstValue(comment);
                if(comment.equals("")){
                    comment = cellEntity.getValue();
                }
            }
            cellEntity.setValue(comment);
        }
    }
}
